package org.lewis.woman.controllers;

import org.json.JSONException;
import org.json.JSONObject;
import org.lewis.common.utils.WebUtils;

import com.tocersoft.framework.core.annotations.Controller;
import com.tocersoft.framework.core.utils.PropertiesUtils;
import com.tocersoft.framework.web.annotations.Mapping;
import com.tocersoft.framework.web.annotations.NameSpace;
import com.tocersoft.framework.web.renderer.Renderer;
import com.tocersoft.framework.web.renderer.TemplateRenderer;
import com.tocersoft.framework.web.renderer.TextRenderer;

@Controller
@NameSpace("/admin")
public class AdminController {

	/**
	 * 转到登录页
	 * 
	 * @return
	 */
	@Mapping("/login")
	public Renderer toLogin() {
		return new TemplateRenderer("/WEB-INF/views/admin_login.html");
	}

	@Mapping("/dologin")
	public Renderer doLogin() throws JSONException {
		String uname = WebUtils.getRequest().getParameter("uname");
		String upwd = WebUtils.getRequest().getParameter("upwd");
		String configName = PropertiesUtils.getValue("admin.username");
		String configPwd = PropertiesUtils.getValue("admin.userpwd");
		JSONObject result = new JSONObject();
		if (configName.equals(uname) && configPwd.equals(upwd)) {
			result.put("CODE", "SUCCESS");
			return new TextRenderer(result.toString());
		}
		result.put("CODE", "ERROR");
		return new TextRenderer(result.toString());
	}

}
